/**
 * 
 */
package cn.ad.server.web.spring.service.actions;

import cn.ad.server.web.spring.model.AdRequest;
import cn.ad.server.web.spring.model.AdResponse;
import cn.ad.server.web.spring.model.BaseTransmitInfo;
import cn.ad.server.web.spring.model.TalkingDataRequest;
import cn.ad.server.web.spring.model.dactive.DactiveAdResponse;
import cn.ad.server.web.spring.service.transmitter.AbstractBaseTransmitter;
import cn.ad.server.web.utils.WebUtils;
import cn.ad.server.web.utils.WebUtils.DeviceInfo;
import cn.ad.server.domain.ad.GuidValueObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * 激活回调-转发处理服务。
 * @author wangwei
 */
//@Component("action-talkingdata")
public class TalkingDataRequestAction extends DactiveRequestAction{
	
	/**
	 * 接入渠道消息解析，针对特定渠道的请求消息解析
	 */
	protected Map<String, AbstractBaseTransmitter> transmitters;
	
	
	private static final Logger LOGGER = LoggerFactory.getLogger(TalkingDataRequestAction.class);

	
	public BaseTransmitInfo convertTransmitInfo(AdRequest request,AdResponse resp){
		if(request instanceof TalkingDataRequest){
			TalkingDataRequest req = (TalkingDataRequest)request;
			BaseTransmitInfo transmitInfo = new BaseTransmitInfo();
			//TODO 转换
			transmitInfo.setAdvertising_id("");
			transmitInfo.setAndroid_id(req.getAndroidid());
			transmitInfo.setCid(req.getCid());
			transmitInfo.setIdfa(req.getIdfa());
			transmitInfo.setImei(req.getImei());
			transmitInfo.setMac(req.getMac());
			return transmitInfo;
		}else{
			throw new ClassCastException();
		}
	}

	/**
	 * 获取第三方补量transmitter
	 * @param adRequest
	 * @return
	 */
	public AbstractBaseTransmitter getTransmitter(AdRequest adRequest,AdResponse resp) {
		TalkingDataRequest req = (TalkingDataRequest)adRequest;
		String ch = req.getCh();
		if(!StringUtils.hasText(ch)){
			return null;
		}
		AbstractBaseTransmitter  thirdPartyAddress= transmitters.get("ch/"+ch);
		return thirdPartyAddress;
	}

	public Map<String, AbstractBaseTransmitter> getTransmitters() {
		return transmitters;
	}

	public void setTransmitters(Map<String, AbstractBaseTransmitter> transmitters) {
		this.transmitters = transmitters;
	}

	public void doProcess(AdRequest adRequest,AdResponse respone){
		TalkingDataRequest req = (TalkingDataRequest)adRequest;
		DactiveAdResponse resp = (DactiveAdResponse)respone;
		resp.setFillChannelId(req.getCh());
		
		DeviceInfo device = new DeviceInfo();
		device.setMac(req.getMac_md5());
		device.setMacori(req.getMac());
		
		device.setIdfa(req.getIdfa_md5());
		device.setIdfaori(req.getIdfa());
		
		device.setImei(req.getImei_md5());
		device.setImeiori(req.getImei());
		
		device.setAnid(req.getAndroidid_md5());
		device.setAnidori(req.getAndroidid());
		
		device = WebUtils.generateMUniqueId(device);
		if(device.getMuniqueType()!=null){
			resp.setMuniqueType(device.getMuniqueType().getName());
			resp.setMuniqueId(device.getMuniqueId());
		}
		
		//获取guid
		GuidValueObject guidData = getGuidData(req.getGuid());
		resp.setGuidData(guidData);
		
	}

}
